/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package views;

import controller.DownloadListener;
import controller.LoadListener;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import xmlaufgabe7.SelectionPanel;
import xmlaufgabe7.UebungData;

/**
 *
 * @author s0527676
 */
public class ContentPanelView extends JPanel implements Observer {

    //MVC Elemente
    private LoadListener loadListener;
    private DownloadListener downloadListener;
    
    //View Elemente
    private JScrollPane scrollPane;
    private JPanel scrollContainer;
    private JPanel buttonPanel;
    private JButton buttonLoad;
    private JButton buttonDownload;
    private JButton buttonExit;
    private ArrayList<SelectionPanel> selectionViews;

    public ContentPanelView(LoadListener loadListener, DownloadListener downloadListener) {
        //MVC
        this.loadListener = loadListener;
        this.downloadListener = downloadListener;
        
        //Init
        selectionViews = new ArrayList<SelectionPanel>();
        scrollContainer = new JPanel();
        scrollContainer.setLayout(new BoxLayout(scrollContainer, BoxLayout.PAGE_AXIS));
        scrollPane = new JScrollPane();
        scrollPane.setPreferredSize(new Dimension(400, 400));
        scrollPane.getViewport().add(scrollContainer);

        buttonLoad = new JButton("Aufgaben laden");
        buttonDownload = new JButton("Dateien herunterladen");
        buttonDownload.setEnabled(false);
        buttonExit = new JButton("Beenden");

        buttonExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SwingUtilities.getWindowAncestor((JButton)e.getSource()).dispose();
            }
        });
        buttonLoad.addActionListener(this.loadListener);
        buttonDownload.addActionListener(this.downloadListener);

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        buttonPanel.add(buttonLoad);
        buttonPanel.add(buttonDownload);
        buttonPanel.add(buttonExit);

        //Layout
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        this.add(scrollPane);
        this.add(buttonPanel);
        
    }
    
    public SelectionPanel getSelectionViewsByIndex(int index) {
        return selectionViews.get(index);
    }
    
    public int getSelectionViewCount() {
        return selectionViews.size();
    }

    public void update(Observable o, Object modelList) {
        generateLoadViews(modelList);
    }

    private void generateLoadViews(Object modelList) {
        ArrayList<UebungData> dataModelList = (ArrayList<UebungData>) modelList;
        
        for(int i = 0; i < dataModelList.size(); i++) {
            selectionViews.add(new SelectionPanel(dataModelList.get(i)));
            scrollContainer.add(selectionViews.get(i));
        }
        
        scrollPane.revalidate();
    }

    /**
     * @return the buttonLoad
     */
    public JButton getButtonLoad() {
        return buttonLoad;
    }

    /**
     * @return the buttonDownload
     */
    public JButton getButtonDownload() {
        return buttonDownload;
    }

}
